package com.tianze.tzkf.function.hardwareInfo;


import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.tianze.tzkf.MainActivity;
import com.tianze.tzkf.R;
import com.tianze.tzkf.base.BaseActivity;
import com.tianze.tzkf.base.BaseFragment;
import com.tianze.tzkf.function.home.HomeActivity;
import com.tianze.tzkf.utils.ApkInfoUtil;
import com.tianze.tzkf.utils.MacAddressUtil;
import com.tianze.tzkf.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.provider.Settings.ACTION_WIFI_SETTINGS;

/**
 * Created by Zcyang on 2018/12/27.
 */
public class HardWareFragment extends BaseFragment  {

    @BindView(R.id.cl_hard_ware_Internet)
    ConstraintLayout cl_hard_ware_Internet; // 网络
    @BindView(R.id.tv_hard_ware_Internet_state)
    TextView tv_hard_ware_Internet_state; // 网络状态
    @BindView(R.id.cl_hard_ware_Bluetooth)
    ConstraintLayout cl_hard_ware_Bluetooth; // 蓝牙
    @BindView(R.id.tv_hard_ware_Bluetooth_state)
    TextView tv_hard_ware_Bluetooth_state; // 蓝牙状态

    @BindView(R.id.tv_hard_ware_Model)
    TextView tv_hard_ware_Model; // 机型
    @BindView(R.id.tv_hard_ware_SM)
    TextView tv_hard_ware_SM; // 本机SM
    @BindView(R.id.tv_hard_ware_IMEI)
    TextView tv_hard_ware_IMEI; // IMEI
    @BindView(R.id.tv_hard_ware_AndroidVersion)
    TextView tv_hard_ware_AndroidVersion; // Android版本
    @BindView(R.id.tv_hard_ware_MACAddress)
    TextView tv_hard_ware_MACAddress; // MAC地址
    @BindView(R.id.tv_hard_ware_ResolvingPower)
    TextView tv_hard_ware_ResolvingPower; // 屏幕分辨率
    @BindView(R.id.tv_hard_ware_Memory)
    TextView tv_hard_ware_Memory; // 内存大小

    String model; // 机型
    String sM; // 本机SM
    String iMEI; // IMEI
    String androidVersion; // Android版本
    String mACAddress; // MAC地址
    String resolvingPower; // 屏幕分辨率
    String memory; // 内存大小
    @BindView(R.id.sw_voice)
    Switch swVoice;
    @BindView(R.id.tv_hard_ware_logout)
    TextView tvHardWareLogout;
    Unbinder unbinder;
    Unbinder unbinder1;
    private AudioManager am;


    public static HardWareFragment newInstance() {

        Bundle args = new Bundle();

        HardWareFragment fragment = new HardWareFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_hard_ware;
    }

    @Override
    protected void initTitle() {
//                getDoNotDisturb();
        am = (AudioManager) getContext()
                .getSystemService(Context.AUDIO_SERVICE);
        int currentVolume  = am.getStreamVolume(AudioManager.STREAM_MUSIC);

        if (currentVolume == 0) {
            swVoice.setChecked(false);
        }else {
            swVoice.setChecked(true);

        }

    }

    @Override
    protected void initData() {

        swVoice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                am.setStreamVolume(AudioManager.STREAM_MUSIC, 10, AudioManager.FLAG_PLAY_SOUND);
                ToastUtils.showToast(getActivity(),"媒体音量已开启");
                }else {
                am.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND);
                ToastUtils.showToast(getActivity(),"媒体音量已关闭");
                }


            }
        });

        model = ApkInfoUtil.getBRAND() + ":" + ApkInfoUtil.getModel();
        androidVersion = ApkInfoUtil.getRELEASE();
        resolvingPower = ApkInfoUtil.getResolvingPower(HardWareFragment.this.getActivity());
        memory = ApkInfoUtil.getAvailMAem(HardWareFragment.this.getActivity());
        if (MacAddressUtil.getLocalMacAddress(HardWareFragment.this.getActivity()) != null) {
            mACAddress = MacAddressUtil.getLocalMacAddress(HardWareFragment.this.getActivity());
        } else {
            mACAddress = "";
        }
        iMEI = ApkInfoUtil.getPhoneIMEI(HardWareFragment.this.getActivity());
        sM = ApkInfoUtil.getPhoneSM(HardWareFragment.this.getActivity());
        initFind();
        initListener();

    }

    public static boolean hasExternalStoragePermission(Context context){
        int perm = context.checkCallingOrSelfPermission(
                "android.permission.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS");
        return perm == PackageManager.PERMISSION_GRANTED;
    }




    private void initFind() {
        if (!model.equals("")) {
            tv_hard_ware_Model.setText(model);
        }
        if (!androidVersion.equals("")) {
            tv_hard_ware_AndroidVersion.setText(androidVersion);
        }
        if (!resolvingPower.equals("")) {
            tv_hard_ware_ResolvingPower.setText(resolvingPower);
        }
        if (!memory.equals("")) {
            tv_hard_ware_Memory.setText(memory + "G");
        }
        if (!mACAddress.equals("")) {
            tv_hard_ware_MACAddress.setText(mACAddress);
        }
        if (iMEI != null && !iMEI.equals("")) {
            tv_hard_ware_IMEI.setText(iMEI);
        }
        if (sM != null && !sM.equals("")) {
            tv_hard_ware_SM.setText(sM);
        }

    }


    private void initListener() {


    }





    private void silentSwitchOn() {
        AudioManager audioManager = (AudioManager) HardWareFragment.this.getActivity().getSystemService(Context.AUDIO_SERVICE);
        if (audioManager != null) {
            audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            audioManager.getStreamVolume(AudioManager.STREAM_RING);
            Log.d("Silent:", "RINGING 已被静音");
        }
    }

    private void silentSwitchOff() {
        AudioManager audioManager = (AudioManager) HardWareFragment.this.getActivity().getSystemService(Context.AUDIO_SERVICE);
        if (audioManager != null) {
            audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            audioManager.getStreamVolume(AudioManager.STREAM_RING);
            Log.d("SilentListenerService", "RINGING 取消静音");
        }
    }




    @OnClick({R.id.cl_hard_ware_Internet, R.id.cl_hard_ware_Bluetooth, R.id.tv_hard_ware_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cl_hard_ware_Internet:
                Intent intent_Internet = new Intent(ACTION_WIFI_SETTINGS);
                startActivity(intent_Internet);
                break;
            case R.id.cl_hard_ware_Bluetooth:
                Intent intent_Bluetooth = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
                startActivity(intent_Bluetooth);
                break;
            case R.id.tv_hard_ware_logout:
                BaseActivity activity = (BaseActivity) getActivity();

                assert activity != null;
                activity.close_WebSocket();
                activity.clearUserInfo();
                activity.goToActivity(MainActivity.class,null);
                activity.removeAllActivity();
               // activity.removeCurrentActivity();
                break;
        }
    }
}
